package cn.edu.neu.studentmsg_java.service.impl;

import cn.edu.neu.studentmsg_java.cons.StateConstants;
import cn.edu.neu.studentmsg_java.dao.LoginDao;
import cn.edu.neu.studentmsg_java.model.Admin;
import cn.edu.neu.studentmsg_java.service.LoginService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginDao loginDao;

    @Override
    public Admin findUserByNameAndPsd(Admin admin) {
        if (ObjectUtils.isEmpty(admin)) throw new RuntimeException(StateConstants.SETTINGS_USER_LOGIN_ACT_OR_LOGIN_PWD_ERROR);
        Admin a = loginDao.selectUserByNameAndPsd(admin);
        return a;
    }

    @Override
    public int addAdmin(Admin admin) {
        int result = loginDao.selectUserByName(admin.getUsername());
        if (result == 0) {
            return loginDao.insertAdmin(admin);
        }
        return 0;
    }
}